package com.mzj.saas.mservice.department.controller;

import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.annotation.RepeatSubmit;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.department.entity.Department;
import com.mzj.saas.mservice.department.service.DepartmentService;
import com.mzj.saas.mservice.department.vo.DepartmentVO;
import com.mzj.saas.mservice.department.vo.PeopleChangeDepartmentVO;
import com.mzj.saas.mservice.sys.vo.ParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/department")
@Api(tags = "组织架构管理")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /*
     *显示当前用户具有权限的组织架构
     */
    @PostMapping(value = "findUserDepartmentList")
    @ResponseBody
    public ResultBean<Object> findUserDepartmentList(@RequestHeader String accessToken, @RequestParam(name = "depName", required = false) String depName) {
        return departmentService.findUserDepartmentList(accessToken, depName);
    }

    /*
     *查询当前部门所在公司层级架构
     */
    @GetMapping(value = "findUserPresentDepartmentList")
    @ResponseBody
    public ResultBean<Object> findUserPresentDepartmentList(@RequestHeader String accessToken) {
        return departmentService.findUserPresentDepartmentList(accessToken);
    }

    /*
     *下级部门顺序调整
     */
    @PostMapping(value = "changeChildDepartmentSort")
    @ResponseBody
    public ResultBean<Object> changeChildDepartmentSort(@RequestHeader String accessToken, @RequestBody Long[] ids) {
        return departmentService.changeChildDepartmentSort(accessToken, ids);
    }

    /*
     *新增/修改成员
     */
    @PostMapping(value = "addDepartmentPeople")
    @ResponseBody
    public ResultBean<Object> addDepartmentPeople(@RequestHeader String accessToken, @RequestBody Map<String, Object> map) {
        return departmentService.addDepartmentPeople(accessToken, map);
    }

    /*
     *显示部门子部门列表
     * id 父级部门Id
     */
    @PostMapping(value = "presentDepartmentChild")
    @ResponseBody
    public ResultBean<Object> presentDepartmentChild(@RequestHeader String accessToken, @RequestParam(name = "id", required = false) Long id) {
        return departmentService.presentDepartmentChild(accessToken, id);
    }

    /*
     *显示部门成员
     */
    @PostMapping(value = "selectDepartmentPeople")
    @ResponseBody
    public ResultBean<Object> selectDepartmentPeople(@RequestHeader String accessToken,
                                                     @RequestParam(defaultValue = "0") Integer pageNumber,
                                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                                     @RequestParam(name = "depId", required = false) Long depId, Integer sort) {
        return departmentService.selectDepartmentPeople(accessToken, pageNumber, pageSize, depId, sort);
    }
    /*
     *查询部门通信录下人员信息
     */
    @GetMapping(value = "selectServiceman")
    @ResponseBody
    @ApiOperation("查询部门通信录下人员信息")
    public ResultBean<Object> selectServiceman(@RequestHeader String accessToken,
                                                     @RequestParam(name = "depId", required = true)
                                                            @ApiParam(name = "depId",value = "部门Id") Long depId) {
        return departmentService.selectServiceman(accessToken, depId);
    }

    /*
     *修改部门名称
     * name 修改部门名称 当前部门id
     */
    @GetMapping(value = "updateDepartmentName")
    @ResponseBody
    public ResultBean<Object> updateDepartmentName(@RequestHeader String accessToken, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "depId", required = false) Long depId) {
        return departmentService.updateDepartmentName(accessToken, name, depId);
    }

    /*
     *部门人员调整部门
     * depId 调整部门编号 id  用户id
     */
    @PostMapping(value = "userUpdateDepartment")
    @ResponseBody
    public ResultBean<Object> userUpdateDepartment(@RequestHeader String accessToken, @RequestBody PeopleChangeDepartmentVO peopleChangeDepartmentVO) {
        return departmentService.userUpdateDepartment(accessToken, peopleChangeDepartmentVO);
    }

    /*
     *修改绿色通道状态0 禁用 1.启用
     */
    @GetMapping(value = "updatePassStatus")
    @ResponseBody
    public ResultBean<Object> updatePassStatus(@RequestHeader String accessToken, @RequestParam(name = "status", required = false) Integer status, @RequestParam(name = "depId", required = false) Long depId) {
        return departmentService.updatePassStatus(accessToken, status, depId);
    }

    /*
     *新增子部门
     */
    @PostMapping(value = "addChildDepartment")
    @ResponseBody
    public ResultBean<Object> addChildDepartment(@RequestHeader String accessToken, @RequestBody DepartmentVO departmentVO) {
        return departmentService.addChildDepartment(accessToken, departmentVO);
    }

    /*
     *批量删除部门成员
     */
    @PostMapping(value = "deleteDepartmentPeople")
    @ResponseBody
    public ResultBean<Object> deleteDepartmentPeople(@RequestHeader String accessToken, @RequestBody String[] userIds) {
        return departmentService.deleteDepartmentPeople(accessToken, userIds);
    }

    /**
     * 导入excel
     */
    @PostMapping(value = "/import")
    @ResponseBody
    public ResultBean<Object> add(@RequestHeader String accessToken,
                                  @RequestParam("file") MultipartFile mfile,
                                  @RequestParam(name = "depId", required = false) Long depId) {
        return departmentService.excelInputdate(accessToken, depId, mfile);
    }

    /**
     * excel模板的下载
     */
    @GetMapping(value = "download")
    public void downLoad(@RequestHeader String accessToken, HttpServletResponse response) {
        this.departmentService.excelDownLoad(accessToken, response);
    }

    /*
     *员工部门权限修改
     */
    @PostMapping(value = "changeDepartmentPeoplePermission")
    @ResponseBody
    public ResultBean<Object> changeDepartmentPeoplePermission(@RequestHeader String accessToken, @RequestBody ParamVO paramVO) {
        return departmentService.changeDepartmentPeoplePermission(accessToken, paramVO);
    }

    /*
     *显示用户拥有的权限部门
     */
    @PostMapping(value = "userPermissionDepartment")
    @ResponseBody
    public ResultBean<Object> userPermissionDepartment(@RequestHeader String accessToken, @RequestParam(name = "userId", required = false) String userId) {
        return departmentService.userPermissionDepartment(accessToken, userId);
    }

    /*
     *显示当前企业设置的新增成员字段信息
     */
    @PostMapping(value = "addDepartmentFieldList")
    @ResponseBody
    public ResultBean<Object> addDepartmentFieldList(@RequestHeader String accessToken) {
        return departmentService.addDepartmentFieldList(accessToken);
    }

    /*
     *部门管理-删除部门
     */
    @PostMapping(value = "deleteDepartment")
    @ResponseBody
    public ResultBean<Object> deleteDepartment(@RequestHeader String accessToken, @RequestParam(name = "depId", required = false) Long depId) {
        return departmentService.deleteDepartment(accessToken, depId);
    }

    /*
     *部门管理-删除部门--批量
     */
    @PostMapping(value = "batchDeleteDepartment")
    @ResponseBody
    public ResultBean<Object> batchDeleteDepartment(@RequestHeader String accessToken, @RequestParam(name = "depId", required = false) List<Long> depIds) {
        return departmentService.batchDeleteDepartment(accessToken, depIds);
    }

    /*
     *部门管理-部门列表显示
     */
    @PostMapping(value = "departmentManagerList")
    @ResponseBody
    public ResultBean<Object> departmentManagerList(@RequestHeader String accessToken, @RequestParam(defaultValue = "0") Integer pageNumber,
                                                    @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(name = "parentId", required = false) Long parentId) {
        return departmentService.departmentManagerList(accessToken, pageNumber, pageSize, parentId);
    }

    /*
     *部门管理-限制人数
     */
    @PostMapping(value = "departmentLimitPeople")
    @ResponseBody
    public ResultBean<Object> departmentLimitPeople(@RequestHeader String accessToken, @RequestParam(name = "depId", required = false) Long depId, @RequestParam(name = "maxNum", required = false) Integer maxNum) {
        return departmentService.departmentLimitPeople(accessToken, depId, maxNum);
    }

    /*
     *部门管理-编辑部门
     */
    @PostMapping(value = "updateDepartmentInfo")
    @ResponseBody
    public ResultBean<Object> updateDepartmentInfo(@RequestHeader String accessToken, @RequestBody DepartmentVO departmentVO) {
        return departmentService.updateDepartmentInfo(accessToken, departmentVO);
    }

    /*
     *导出部门成员信息
     */
    @PostMapping(value = "departmentPeopleExport")
    @ResponseBody
    public void departmentPeopleExport(@RequestHeader String accessToken, @RequestParam(name = "depId", required = false) Long depId) {
        this.departmentService.departmentPeopleExport(accessToken, depId);
    }

    /**
     *获取未开门信息推送信息
     * @param accessToken
     * @return
     */
    @GetMapping("/getOpenPushInfo")
    @CheckAuth
    @ResponseBody
    public ResultBean<Department> getOpenPushInfo(@RequestHeader String accessToken){
        return departmentService.getOpenPushInfo(accessToken);
    }

    /**
     * 编辑未开门信息推送信息
     * @param department
     * @return
     */
    @GetMapping("/updateOpenPushInfo")
    @CheckAuth
    @RepeatSubmit
    @ResponseBody
    public ResultBean<Object> updateOpenPushInfo(@RequestHeader String accessToken, Department department) {
        return departmentService.updateOpenPushInfo(accessToken, department);
    }

    /*
     *查询企业信息
     */
    @GetMapping(value = "info")
    @ResponseBody
    public ResultBean<Map<String, Object>> info(@RequestParam(name = "id") Long id) {
        return departmentService.info(id);
    }
}
